package com.qu.waibao.my.activity;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qu.waibao.AppConfig;
import com.qu.waibao.MainActivity;
import com.qu.waibao.R;
import com.qu.waibao.base.BaseActivity;
import com.qu.waibao.base.BaseViewPagerSfragment;
import com.qu.waibao.my.fragment.AcceptOrderViewPagerFragment;
import com.qu.waibao.my.fragment.PublishOrderFragment;
import com.qu.waibao.my.fragment.PublishOrderViewPageFragment;
import com.qu.waibao.network.ActionUtil;
import com.qu.waibao.search.OrderSearchActivity;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class PublishOrderActivity extends BaseActivity implements View.OnClickListener{
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.iv_publish)
    ImageView iv_publish;
    @BindView(R.id.tab_nav)
    protected TabLayout mTabNav;
    @BindView(R.id.base_viewPager)
    protected ViewPager mBaseViewPager;
    private String type = "";
    @Override
    protected boolean initBundle(Bundle bundle) {
        if(bundle != null){
            type = bundle.getString("type","more");
        }
        return super.initBundle(bundle);
    }
    @Override
    protected int getContentView() {
        return R.layout.activity_publish_order;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        int card = AppConfig.getInt("card");
        if(card == 1){
            tv_title.setText("我的订单");
        }else {
            tv_title.setText("发布的订单");
        }
        iv_publish.setImageResource(R.drawable.sc_fangdajing);
        BaseViewPagerAdapter adapter = new BaseViewPagerAdapter(getSupportFragmentManager(), getPagers());
        mBaseViewPager.setAdapter(adapter);
        mTabNav.setupWithViewPager(mBaseViewPager);
        if(type.equals("finished")){
            mBaseViewPager.setCurrentItem(4,true);
        }else if(type.equals("finishing")){
            mBaseViewPager.setCurrentItem(3,true);
        }else if(type.equals("unpaid")){
            mBaseViewPager.setCurrentItem(1,true);
        }else if(type.equals("qiang")){
            mBaseViewPager.setCurrentItem(2,true);
        }else {
            mBaseViewPager.setCurrentItem(0, true);
        }
    }

    @Override
    protected void initData() {
        super.initData();
    }
    @OnClick({R.id.ll_back,R.id.iv_publish})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_back:
               onBackPressed();
                break;
            case R.id.iv_publish:
                Intent intent = new Intent(this, OrderSearchActivity.class);
                intent.putExtra("ordertype","1");
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        finish();
        super.onBackPressed();
    }
    private PagerInfo[] getPagers() {
        return new PagerInfo[]{
                new PagerInfo("全部",PublishOrderFragment.class,getBundle("")),
                new PagerInfo("待支付",PublishOrderFragment.class,getBundle("待支付")),
                new PagerInfo("待抢单",PublishOrderFragment.class,getBundle("待抢单")),
                new PagerInfo("待完成",PublishOrderFragment.class,getBundle("待完成")),
                new PagerInfo("已完成",PublishOrderFragment.class,getBundle("已完成"))
        };
    }

    public static class PagerInfo {
        private String title;
        private Class<?> clx;
        private Bundle args;

        public PagerInfo(String title, Class<?> clx, Bundle args) {
            this.title = title;
            this.clx = clx;
            this.args = args;
        }
    }
    public class BaseViewPagerAdapter extends FragmentPagerAdapter {
        private PagerInfo[] mInfoList;
        private Fragment mCurFragment;

        public BaseViewPagerAdapter(FragmentManager fm, PagerInfo[] infoList) {
            super(fm);
            mInfoList = infoList;
        }

        @Override
        public void setPrimaryItem(ViewGroup container, int position, Object object) {
            super.setPrimaryItem(container, position, object);
            if (object instanceof Fragment) {
                mCurFragment = (Fragment) object;
            }
        }

        public Fragment getCurFragment() {
            return mCurFragment;
        }

        @Override
        public Fragment getItem(int position) {
            PagerInfo info = mInfoList[position];
            return Fragment.instantiate(PublishOrderActivity.this, info.clx.getName(), info.args);
        }

        @Override
        public int getCount() {
            return mInfoList.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mInfoList[position].title;
        }

        @Override
        public int getItemPosition(Object object) {
            return PagerAdapter.POSITION_NONE;
        }
    }
    private Bundle getBundle(String type) {
        Bundle bundle = new Bundle();
        bundle.putString("type",type);
        return bundle;
    }
}
